
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections;
using System.Text;

namespace Friendster.Service
{
    public sealed class Album : FriendsterEntity
    {
        // Fields

        private bool _createdDateTransformed;
        private bool _modifiedDateTransformed;

        // Methods
        internal Album(Hashtable albumState): base((Hashtable)albumState["album"])
        {
            albumState = (Hashtable)albumState["album"];
        }

        private DateTime TransformCreatedDate()
        {
            DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string seconds = (string)(base.GetState<string>("created"));
            if (!string.IsNullOrEmpty(seconds))
            {
                time = time.AddSeconds(Convert.ToDouble(seconds));
                base["created"] = time;
            }
            else
                base["created"] = "";

            this._createdDateTransformed = true;
            return (DateTime)base["created"];
        }

        private DateTime TransformModifiedDate()
        {
            DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string seconds = (string)(base.GetState<string>("modified"));
            if (!string.IsNullOrEmpty(seconds))
            {
                time = time.AddSeconds(Convert.ToDouble(seconds));
                base["modified"] = time;
            }
            else
                base["modified"] = "";
            this._modifiedDateTransformed = true;
            return (DateTime)base["modified"];
        }

        // Properties
        /// <summary>
        /// Returns the total number of photos in this album.
        /// </summary>
        public int Count
        {
            get
            {
                string size = base.GetState<string>("size");
                return Int32.Parse(size);
            }
        }
        /// <summary>
        /// Returns the pid of the albums cover photo
        /// </summary>
        public string CoverID
        {
            get
            {
                return base.GetState<string>("cover_pid") ?? string.Empty;
            }
        }
        /// <summary>
        /// Returns datetime object of when this album was created
        /// </summary>
        public DateTime CreatedDate
        {
            get
            {
                if (!this._createdDateTransformed)
                {
                    return this.TransformCreatedDate();
                }
                return (DateTime)base["created"];
            }
        }
        /// <summary>
        /// Returns the description of this album
        /// </summary>
        public string Description
        {
            get
            {
                return (base.GetState<string>("description") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns the album ID
        /// </summary>
        public string ID
        {
            get
            {
                return (base.GetState<string>("aid") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns a link to the albums' page
        /// </summary>
        public string LinkUrl
        {
            get
            {
                return (base.GetState<string>("link") ?? string.Empty);
            }
        }

        /// <summary>
        /// Returns datetime object of when this album was last modified.
        /// </summary>
        public DateTime ModifiedDate
        {
            get
            {
                if (!this._modifiedDateTransformed)
                {
                    return this.TransformModifiedDate();
                }
                return (DateTime)base["modified"];
            }
        }
        /// <summary>
        /// Returns the name of the album
        /// </summary>
        public string Name
        {
            get
            {
                return (base.GetState<string>("name") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns userID of album owner
        /// </summary>
        public string OwnerID
        {
            get
            {
                return (base.GetState<string>("owner") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns a boolean value indicating whether the album is public or private
        /// </summary>
        public bool IsPrivate
        {
            get
            {
                if (base.GetState<string>("isprivate") == "0")
                    return false;
                else
                    return true;
            }
        }
    }
}